<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>数组解构赋值</title>
</head>

<body>
    <div>
        数组解构赋值，通过“模式匹配”，将数组元素赋值给本地变量。 数组元素是依次排列，解构取值是按照位置。 数组的解构，使用的就是“[]”
    </div>
    <pre>
          let [a,b] = [1,2];
          console.log(a);  // 1
          console.log(b);  // 2
      </pre
    >
    <hr />
    <div>解构表达式等号右侧如果没有值，左侧解构对应的变量是 undefined</div>
    <pre>
      let [m, n] = ["alpha"];
      console.log(m); // "alpha"
      console.log(n); // undefined
    </pre>
    <hr />
    <div>如果右侧不是数组（可迭代结构），则解构报错</div>
    <pre>
            let [demo] =1;   //Uncaught TypeError: 1 is not iterable
            console.log(demo)   
      </pre
    >
    <hr />
    <div>Set类型也可以解构</div>
    <pre>
      let [x,y,z] = new Set([1,2,3])
      console.log(x); // 1
      console.log(y); // 2
      console.log(z); // 3
  </pre
    >
    <hr />
    <div>解构默认值使用=赋值, 当数组元素严格等于undefined,解构启用默认值</div>
    <pre>
              let [aa=1,bb]=[3,4]   // aa=3 , bb=4 
              let [cc,dd=1]=[];     //cc=undefined ,dd=1
            </pre
    >
  </body>
</html>
<script>
  let [a, b] = [1, 2];
  console.log(a); // 1
  console.log(b); // 2
  console.log("------------------");
  let [m, n] = ["alpha"];
  console.log(m); // "alpha"
  console.log(n); // undefined
  console.log("------------------");
  //let [demo] = 1; //Uncaught TypeError: 1 is not iterable
  // console.log(demo);
  console.log("------------------");
  let [x, y, z] = new Set([1, 2, 3]);
  console.log(x); // 1
  console.log(y); // 2
  console.log(z); // 3
  console.log("------------------");
  let [aa = 1, bb] = [3, 4]; // aa=3 , bb=4
  let [cc, dd = 1] = []; //cc=undefined ,dd=1
</script>